package com.dev;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.TextMore;
import java.io.File;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.Paint;
import android.graphics.Canvas;
import android.graphics.BitmapFactory;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff;

public class QueryMakeShortcut extends LinearLayout implements OnClickListener,OnEditorActionListener {
    WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
    WindowManager.LayoutParams poplp=new WindowManager.LayoutParams();
    MainActivity ma;
    ImageView icon;
    LinearLayout titlebar;
    LinearLayout queryType;
    LinearLayout queryOpera;
    TextView title;
    IconViewClose close;
    ShowSoftInput ssi;
    UnderlineSingleLineEditor name;
    TextView tb1,tb2;
    TextView it1,it2;
    TextMore tm1,tm2;
    String type1=" 桌面快捷方式";
    String type2=" 图标快捷方式";
    String type;
    String oper1=" 常规打开";
    String oper2=" 打开所在目录";
    String oper;
    int resid;
    Bitmap iconbm;
    public QueryMakeShortcut(MainActivity ma){
        super(ma);
        this.ma=ma;
        setOrientation(1);
        setBackground(new GUI.BorderDrawable());
        close=new IconViewClose(ma);
        close.setOnClickListener(this);
        name=new UnderlineSingleLineEditor(ma);
        name.setOnEditorActionListener(this);
        name.setPadding(9,0,0,0);
        poplp.height=-2;
        poplp.width=ma.wp*2/3;
        poplp.gravity=Gravity.LEFT|Gravity.TOP;
        poplp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
        icon=new ImageView(ma);
        ssi=new ShowSoftInput(ma);
        ssi.setOnClickListener(this);
        TextView t0=new TextView(ma);
        TextView t1=new TextView(ma);
        TextView t2=new TextView(ma);
        TextView t3=new TextView(ma);
        t0.setText(" 图标 ");
        t0.setGravity(Gravity.CENTER_VERTICAL);
        t0.setTextSize(1,13);
        t1.setText(" 名称 ");
        t1.setGravity(Gravity.CENTER_VERTICAL);
        t1.setTextSize(1,13);
        t2.setText(" 类型 ");
        t2.setGravity(Gravity.CENTER_VERTICAL);
        t2.setTextSize(1,13);
        t3.setText(" 操作 ");
        t3.setGravity(Gravity.CENTER_VERTICAL);
        t3.setTextSize(1,13);
        tb1=new TextView(ma);
        tb1.setGravity(Gravity.CENTER);
        tb1.setOnClickListener(this);
        tb1.setText(" 管理 ");
        tb1.setTextColor(0xff00bbff);
        tb1.setTextSize(1,13);
        tb2=new TextView(ma);
        tb2.setGravity(Gravity.CENTER);
        tb2.setOnClickListener(this);
        tb2.setText(" 确定 ");
        tb2.setTextColor(0xff00bbff);
        tb2.setTextSize(1,13);
        title=new TextView(ma);
        title.setText(" 创建快捷方式");
        title.setGravity(Gravity.CENTER_VERTICAL);
        title.setTextSize(1,13);
        titlebar=new LinearLayout(ma);
        titlebar.setBackground(new GUI.BorderDrawable());
        titlebar.setOrientation(0);
        titlebar.addView(title,new LayoutParams(0,-1,1f));
        titlebar.addView(ssi);
        titlebar.addView(close);
        tm1=new TextMore(ma);
        tm1.setOnClickListener(this);
        tm1.setTextSize(1,13);
        tm2=new TextMore(ma);
        tm2.setOnClickListener(this);
        tm2.setTextSize(1,13);
        LinearLayout linear0=new LinearLayout(ma);
        LinearLayout linear1=new LinearLayout(ma);
        LinearLayout linear2=new LinearLayout(ma);
        LinearLayout linear3=new LinearLayout(ma);
        LinearLayout linear4=new LinearLayout(ma);
        linear0.setOrientation(0);
        linear0.addView(t0,-2,-1);
        linear0.addView(icon,new LayoutParams(0,-1,1f));
        linear1.setOrientation(0);
        linear1.addView(t1,-2,-1);
        linear1.addView(name,new LayoutParams(0,-1,1f));
        linear2.setOrientation(0);
        linear2.addView(t2,-2,-1);
        linear2.addView(tm1,new LayoutParams(0,-1,1f));
        linear3.setOrientation(0);
        linear3.addView(t3,-2,-1);
        linear3.addView(tm2,new LayoutParams(0,-1,1f));
        linear4.setOrientation(0);
        linear4.addView(tb1,-2,-1);
        linear4.addView(tb2,-2,-1);
        addView(titlebar,-1,ma.dialogTitleBarHeight);
        addView(linear0,-1,(int)(34*ma.dp));
        addView(linear1,-1,ma.dialogTitleBarHeight);
        addView(linear2,-1,ma.dialogTitleBarHeight);
        addView(linear3,-1,ma.dialogTitleBarHeight);
        addView(linear4,-1,ma.dialogRowHeight);
    }
    Bitmap getbm(int resid,boolean hidden){
        Paint p=new Paint();
        if(hidden)p.setColorFilter(IconView.unable);
        Bitmap bm=BitmapFactory.decodeResource(getResources(),resid);
        Bitmap result=Bitmap.createBitmap(bm.getWidth(),bm.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas cvs=new Canvas(result);
        cvs.drawColor(0xffffffff);
        cvs.drawBitmap(bm,0,0,p);
        return result;
    }
    File destination;
    public void show(File destination){
        this.destination=destination;
        wlp.width=ma.wp*2/3;
        wlp.height=-2;
        if(type==null||type==type1){
            tm1.setText(type1);
        }else{
            tm1.setText(type2);
        }
        if(oper==null||oper==oper1){
            tm2.setText(oper1);
        }else{
            tm2.setText(oper2);
        }
        String name=destination.getName();
        if(destination.isDirectory()){
            if(name.charAt(0)=='.'){
                iconbm=getbm(R.drawable.folder,true);
            }else{
                iconbm=getbm(R.drawable.folder,false);
            }
        }else{
            int dot=name.lastIndexOf('.');
            if(dot<0){
                iconbm=getbm(R.drawable.file_type_unknown,false);
            }else if(dot==0){
                iconbm=getbm(R.drawable.file_type_unknown,true);
            }else if(dot==name.length()-1){
                iconbm=getbm(R.drawable.file_type_unknown,false);
            }else{
                String suffix=name.substring(dot+1,dot+1);
                boolean b;
                if(name.indexOf('.')==0){
                    b=true;
                }else{
                    b=false;
                }
                switch(suffix){
                    case "apk":
                        iconbm=getbm(R.drawable.format_apk,b);
                        break;
                    case "png":
                        iconbm=getbm(R.drawable.format_picture,b);
                        break;
                    case "jpg":
                        iconbm=getbm(R.drawable.format_picture,b);
                        break;
                    case "html":
                        iconbm=getbm(R.drawable.file_type_html,b);
                        break;
                    case "htm":
                        iconbm=getbm(R.drawable.file_type_html,b);
                        break;
                    case "pdf":
                        iconbm=getbm(R.drawable.ic_file_pdf,b);
                        break;
                    case "zip":
                        iconbm=getbm(R.drawable.ic_zip_box,b);
                        break;
                    case "tar":
                        iconbm=getbm(R.drawable.ic_zip_box,b);
                        break;
                    case "so":
                        iconbm=getbm(R.drawable.gear,b);
                        break;
                    case "mp3":
                        iconbm=getbm(R.drawable.music,b);
                        break;
                    case "m4a":
                        iconbm=getbm(R.drawable.music,b);
                        break;
                    case "mp4":
                        iconbm=getbm(R.drawable.video,b);
                        break;
                    case "svg":
                        iconbm=getbm(R.drawable.file_type_svg,b);
                        break;
                    case "c":
                        iconbm=getbm(R.drawable.file_type_c,b);
                        break;
                    case "java":
                        iconbm=getbm(R.drawable.file_type_java,b);
                        break;
                    case "txt":
                        iconbm=getbm(R.drawable.file_type_txt,b);
                        break;
                    case "xml":
                        iconbm=getbm(R.drawable.xml,b);
                        break;
                    case "gif":
                        iconbm=getbm(R.drawable.gif,b);
                        break;
                    default:
                        iconbm=getbm(R.drawable.file_type_unknown,b);
                }
            }
            
        }
        icon.setImageBitmap(iconbm);
        QueryMakeShortcut.this.name.setText(name);
        ma.wm.addView(this,wlp);
    }
    @Override
    public void onClick(View v) {
        if(v==close){
            ma.wm.removeView(this);
        }else if(v==tb1){
            if(ma.qms==null)ma.qms=new QueryManageShortcut(ma);
            ma.qms.show();
        }else if(v==tb2){
            String usrinput=name.getEditableText().toString();
            /*Intent intent=new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
             intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,com.dev.R.string.packageName);
             ma.startActivityForResult(intent,20);*/
            if(usrinput.isEmpty()){
                Toast.makeText(ma,"名称不能为空",0).show();
            }else if(usrinput.trim().isEmpty()){
                Toast.makeText(ma,"名称不能由空白符组成",0).show();
            }else if(type==null||type==type1){
                usrinput=usrinput.trim();
                if(ma.shortcutManager==null)ma.shortcutManager=ma.getSystemService(ShortcutManager.class);
                ShortcutManager sm=ma.shortcutManager;
                if(sm.isRequestPinShortcutSupported()){

                    ShortcutInfo.Builder sib=new ShortcutInfo.Builder(ma,"test");
                    sib.setShortLabel(usrinput);
                    //sib.setLongLabel(usrinput);
                    sib.setIcon(Icon.createWithBitmap(iconbm));
                    Intent action=new Intent(Intent.ACTION_VIEW,Uri.parse("http://bing.com"));
                    sib.setIntent(action);
                    ShortcutInfo si=sib.build();
                    Intent callback=sm.createShortcutResultIntent(si);

                    PendingIntent pi=PendingIntent.getBroadcast(ma,0,callback,0);
                    sm.requestPinShortcut(si,pi.getIntentSender());
                    ma.wm.removeView(this);
                }else{
                    Toast.makeText(ma,"设备不支持图标快捷方式",0).show();
                }
            }else if(type==type2){
                usrinput=usrinput.trim();
                if(ma.shortcutManager==null)ma.shortcutManager=ma.getSystemService(ShortcutManager.class);
                ShortcutManager sm=ma.shortcutManager;
                ShortcutInfo.Builder sib=new ShortcutInfo.Builder(ma,"id");
                sib.setShortLabel(usrinput);
                sib.setLongLabel(usrinput);
                sib.setIcon(Icon.createWithResource(ma,com.dev.R.drawable.file_type_txt));
                Intent action=new Intent(Intent.ACTION_VIEW,Uri.parse("http://bing.com"));
                sib.setIntent(action);
                sm.pushDynamicShortcut(sib.build());
                ma.wm.removeView(this);
            }else{

            }
        }else if(v==ssi){
            if(name.requestFocus()){
                name.selectAll();
                ma.imm.showSoftInput(name,0);
            }
        }else if(v==tm1){
            if(queryType==null)queryType=new QueryType(ma);
            int[] loc=new int[2];
            tm1.getLocationOnScreen(loc);
            poplp.x=loc[0];
            poplp.y=loc[1]+tm1.getHeight();
            ma.wm.addView(queryType,poplp);
        }else if(v==tm2){
            if(queryOpera==null)queryOpera=new QueryOperation(ma);
            int[] loc=new int[2];
            tm2.getLocationOnScreen(loc);
            poplp.x=loc[0];
            poplp.y=loc[1]+tm2.getHeight();
            ma.wm.addView(queryOpera,poplp);
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        return false;
    }
    class QueryType extends LinearLayout implements OnClickListener{
        QueryType(MainActivity ma){
            super(ma);
            setOrientation(1);
            setBackground(new GUI.BorderDrawable());
            it1=new TextView(ma);
            it1.setBackground(new GUI.Pressed());
            it1.setGravity(Gravity.CENTER_VERTICAL);
            it1.setOnClickListener(this);
            it1.setText(type1);
            it1.setTextColor(0xff000000);
            it1.setTextSize(1,13);
            it2=new TextView(ma);
            it2.setBackground(new GUI.Pressed());
            it2.setGravity(Gravity.CENTER_VERTICAL);
            it2.setOnClickListener(this);
            it2.setText(type2);
            it2.setTextColor(0xff000000);
            it2.setTextSize(1,13);
            addView(it1,-1,ma.dialogRowHeight);
            addView(it2,-1,ma.dialogRowHeight);
        }
        @Override
        public boolean onTouchEvent(MotionEvent event){
            int xx=(int)event.getX();
            int yy=(int)event.getY();
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(xx<0||xx>getWidth()||yy<0||yy>getHeight()){
                        ma.wm.removeView(this);
                    }
            }
            return super.onTouchEvent(event);
        }
        @Override
        public void onClick(View v) {
            if(v==it1){
                type=type1;
                tm1.setText(type1);
                ma.wm.removeView(this);
            }else if(v==it2){
                type=type2;
                tm1.setText(type2);
                ma.wm.removeView(this);
            }
        }
    }
    class QueryOperation extends LinearLayout implements OnClickListener{
        TextView op1,op2;
        QueryOperation(MainActivity ma){
            super(ma);
            setOrientation(1);
            setBackground(new GUI.BorderDrawable());
            op1=new TextView(ma);
            op1.setBackground(new GUI.Pressed());
            op1.setGravity(Gravity.CENTER_VERTICAL);
            op1.setOnClickListener(this);
            op1.setText(oper1);
            op1.setTextColor(0xff000000);
            op1.setTextSize(1,13);
            op2=new TextView(ma);
            op2.setBackground(new GUI.Pressed());
            op2.setGravity(Gravity.CENTER_VERTICAL);
            op2.setOnClickListener(this);
            op2.setText(oper2);
            op2.setTextColor(0xff000000);
            op2.setTextSize(1,13);
            addView(op1,-1,ma.dialogRowHeight);
            addView(op2,-1,ma.dialogRowHeight);
        }

        @Override
        public void onClick(View v) {
            if(v==op1){
                oper=oper1;
                tm2.setText(oper1);
                ma.wm.removeView(this);
            }else if(v==op2){
                oper=oper2;
                tm2.setText(oper2);
                ma.wm.removeView(this);
            }
        }
        @Override
        public boolean onTouchEvent(MotionEvent event){
            int xx=(int)event.getX();
            int yy=(int)event.getY();
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(xx<0||xx>getWidth()||yy<0||yy>getHeight()){
                        ma.wm.removeView(this);
                    }
            }
            return super.onTouchEvent(event);
        }
    }
}
